package com.zzl.car.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzl.car.entity.CarCheckRecord;
import com.zzl.car.entity.Notice;
import com.zzl.car.entity.Worker;
import com.zzl.car.exception.ZdpException;
import com.zzl.car.mapper.ZzwWorkerMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ZzwWorkerService {

    @Autowired
    private ZzwWorkerMapper workerMapper;

    public PageInfo<Worker> getWorker(String name, Integer state, int currentPage){
        int pageAmount = 8;
        PageInfo<Worker> pageInfo = null;
        List<Worker> w = new ArrayList<>();

        PageHelper.startPage(currentPage,pageAmount);
        w = workerMapper.getWorker(name,state);

        pageInfo = new PageInfo<>(w);
        pageInfo.setList(w);

        return pageInfo;

    }

    public String deleteWorker(Integer id){

        List<CarCheckRecord> l = workerMapper.getCarCheckRecord();
        for (CarCheckRecord c : l) {
            if(c.getId() == id){
                throw new ZdpException(2929,"该员工信息包含在其他信息表中，不可删除");
            }
        }

        if(workerMapper.deleteWorker(id) > 0)
            return "1";
        else
            throw new ZdpException(2222,"删除失败");
    }

    public String addWorker(Worker w){
        if(workerMapper.addWorker(w) > 0)
            return "1";
        else
            throw new ZdpException(2222,"添加员工失败");
    }

    public String modifyWorker(Worker w){
        if(workerMapper.modifyWorker(w) > 0)
            return "1";
        else
            throw new ZdpException(2222,"修改员工信息失败");
    }
}
